package com.lgcns.andteam2.todaymenu;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.lgcns.andteam2.todaymenu.db.QueryHelper;
import com.lgcns.andteam2.todaymenu.item.TodayMenu;

/**
 * 오늘 메뉴 데이터&View를 가지는 adapter
 */
public class TodayMenuAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<TodayMenu> mMenuList;
    private LayoutInflater inflater;
    private SimpleDateFormat df = new SimpleDateFormat("M.d(E)");

    public TodayMenuAdapter(Context context) {
        super();
        this.mContext = context;
        mMenuList = new ArrayList<TodayMenu>(); // adapter 생성시에는 빈 데이터 생성
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    /**
     * Adapter의 데이터를 query한다.
     * @param menuDate
     * @param gubunCode
     */
    public void refreshDataSet(String menuDate, String gubunCode) {
        QueryHelper.populateTodayMenuData(mContext, menuDate, gubunCode, mMenuList);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mMenuList.size();
    }

    @Override
    public Object getItem(int position) {
        return mMenuList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        
        if(convertView == null) {
            convertView = inflater.inflate(R.layout.today_row, parent, false);
        }
        
        TextView corner = (TextView)convertView.findViewById(R.id.corner);
        TextView menuName = (TextView)convertView.findViewById(R.id.menu_name);
        TextView cal = (TextView)convertView.findViewById(R.id.cal);
        TextView sideMenu = (TextView)convertView.findViewById(R.id.side_menu);
        Button survey = (Button)convertView.findViewById(R.id.survey);
        
        TodayMenu menu = mMenuList.get(position);
        if(!TextUtils.isEmpty(menu.getCorner())) {
            corner.setVisibility(View.VISIBLE);
            corner.setText(menu.getCorner());
        } else {
            corner.setVisibility(View.GONE);
        }
        
        menuName.setText(menu.getMenuName());
        if(menu.getCal() > 0) {
            cal.setText(String.format(mContext.getString(R.string.cal), Util.formatNumber(menu.getCal())) + " "); // " "은 italic체로 할 경우 맨 끝 문자가 약간 잘리는 것 방지
            cal.setVisibility(View.VISIBLE);
        } else {
            cal.setVisibility(View.INVISIBLE);
        }
        sideMenu.setText(menu.getSideMenuList(mContext));
        
        final int sPlaceId = menu.getPlaceId();
        
        StringBuffer tmp = new StringBuffer();
        tmp.append(mContext.getString(R.string.survey)).append(": ");
        tmp.append(df.format(Util.parseCalendar(menu.getMenuDate()).getTime())).append(" - ");
        if(!TextUtils.isEmpty(menu.getCorner())) {
            tmp.append(menu.getCorner()).append("/");
        }
        tmp.append(menu.getMenuName());
        final String subject = tmp.toString();
        
        survey.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 담당자에게 설문 보내기
                String[] email = QueryHelper.getDietitianEmail(mContext, sPlaceId);
                
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/html");
                intent.putExtra(Intent.EXTRA_EMAIL, email);
                intent.putExtra(Intent.EXTRA_SUBJECT, subject);
                intent.putExtra(Intent.EXTRA_TEXT, mContext.getString(R.string.survey_opinion));
                mContext.startActivity(intent);
            }
        });
        
        return convertView;
    }
}